package com.example.bingobox.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.SpannableStringBuilder;
import android.text.Spannable;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.bingobox.R;
import com.example.bingobox.model.State.LocationMark;

import java.util.List;

public class LocationMarkListAdapter extends RecyclerView.Adapter<LocationMarkListAdapter.ViewHolder> {
    private final List<LocationMark> marks;
    private final Context context;

    public LocationMarkListAdapter(Context context, List<LocationMark> marks) {
        this.context = context;
        this.marks = marks;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_location_mark, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        LocationMark mark = marks.get(position);
        
        SpannableStringBuilder builder = new SpannableStringBuilder();
        
        // 添加地址
        String address = mark.getLocation().getAddress() != null ? 
            mark.getLocation().getAddress() : "";
        builder.append(address);
        builder.append("\n");
        
        // 添加名称
        builder.append(mark.getLocation().getName());
        builder.append("\n");
        
        // 添加标签
        builder.append(mark.getType());
        
        // 设置地址为灰色小字
        builder.setSpan(
            new ForegroundColorSpan(Color.GRAY),
            0,
            address.length(),
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
        );
        builder.setSpan(
            new RelativeSizeSpan(0.8f),
            0,
            address.length(),
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
        );
        
        // 设置标签为蓝色
        int tagStart = builder.length() - mark.getType().length();
        builder.setSpan(
            new ForegroundColorSpan(Color.BLUE),
            tagStart,
            builder.length(),
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
        );
        
        holder.locationText.setText(builder);
    }

    @Override
    public int getItemCount() {
        return marks.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        final TextView locationText;

        ViewHolder(View view) {
            super(view);
            locationText = view.findViewById(R.id.location_text);
        }
    }
} 